#lang racket

(require "utilities.scm")
(require "syntax-object.scm")

(provide parse-file)

(define (syntax-metadata stx)
  (list (cons 'src (syntax-source stx))
        (cons 'lin (syntax-line stx))
        (cons 'col (syntax-column stx))))

(define (process-syntax-object stx)
  (let ((e (if (syntax? stx) (syntax-e stx) stx)))
    (cond ((atomic? e)
	   (syx 'atomic e (syntax-metadata stx)))
          ((symbol? e)
	   (syx 'id e (syntax-metadata stx)))
          ((pair? e)
	   (cons (process-syntax-object (car e))
		 (process-syntax-object (cdr e))))
          ((null? e) '())
          (else (error "unknown syntax object" stx)))))

(define (parse-file filename)
  (call-with-input-file filename
    (lambda (in)
      (port-count-lines! in)
      (let loop ()
        (let ((object (read-syntax filename in)))
          (if (eof-object? object)
              '()
              (cons (process-syntax-object object) (loop))))))))
